package com.rxb.blog.service.impl;

import com.rxb.blog.entity.ArticleContentEntity;
import com.rxb.blog.entity.ArticleInfoEntity;
import com.rxb.blog.entity.ArticlePraiseEntity;
import com.rxb.blog.mapper.ArticleContentMapper;
import com.rxb.blog.mapper.ArticleInfoMapper;
import com.rxb.blog.mapper.ArticlePraiseMapper;
import com.rxb.blog.service.ArticleInfoService;
import com.rxb.blog.vo.ArticleInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-02-26 16:49
 * Description：<描述>
 */
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ArticleInfoMapper articleInfoMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticlePraiseMapper articlePraiseMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public void insertArticle(ArticleInfoVo articleInfoVo){
        ArticleInfoEntity articleInfoEntity = new ArticleInfoEntity();
        articleInfoEntity.setCreate_by(articleInfoVo.getUsername());
        articleInfoEntity.setUpdate_by(articleInfoVo.getUsername());
        articleInfoEntity.setTitle(articleInfoVo.getTitle());
        List<String> articleTypes = articleInfoVo.getArticleTypes();
        String join = String.join(",", articleTypes);
        articleInfoEntity.setType(join);
        articleInfoEntity.setTop(articleInfoVo.getTop());
        articleInfoEntity.setUsername(articleInfoVo.getUsername());
        articleInfoEntity.setOriginal_link(articleInfoVo.getOriginalLink());
        articleInfoEntity.setOriginal(articleInfoVo.getOriginal());
        articleInfoEntity.setPrivate_blog(articleInfoVo.getPrivateBlog());
        articleInfoEntity.setFlag(articleInfoVo.getFlag());
        if(null == articleInfoVo.getId()){
            articleInfoMapper.insertArticleInfo(articleInfoEntity);
        }else{
            articleInfoEntity.setId(articleInfoVo.getId());
            articleInfoMapper.updateArticleInfo(articleInfoEntity);
        }

        ArticleContentEntity articleContentEntity = new ArticleContentEntity();
        articleContentEntity.setArticle_info_id(articleInfoEntity.getId());
        articleContentEntity.setContent(articleInfoVo.getContent());
        articleContentEntity.setCreate_by(articleInfoVo.getUsername());
        articleContentEntity.setUpdate_by(articleInfoVo.getUsername());
        if(null == articleInfoVo.getId()){
            articleContentMapper.insertArticleContent(articleContentEntity);
        }else{
            articleContentMapper.updateArticleContent(articleContentEntity);
        }
    }

    @Override
    public Map getMyArticleList(ArticleInfoVo articleInfoVo) {
        Map map = new HashMap();
       // articleInfoVo.setPageStartNum((articleInfoVo.getCurrentPage()-1)*articleInfoVo.getPageSize());
        List<ArticleInfoEntity> articles = articleInfoMapper.getMyArticleList(articleInfoVo);
        List<ArticleInfoVo> articlesVo = new ArrayList<>();
        articles.forEach(article ->{
            ArticleInfoVo articleInfoVo1 = new ArticleInfoVo();
            if(article.getTop() == 1){
                articleInfoVo1.setConcern("置顶");
            }else{
                if(article.getPrivate_blog()==1){
                    articleInfoVo1.setConcern("私密");
                }else{
                    if(article.getBlog_status() == 0){
                        articleInfoVo1.setConcern("草稿");
                    }
                }
            }
            articleInfoVo1.setUsername(article.getUsername());
            articleInfoVo1.setOriginal(article.getOriginal());
            articleInfoVo1.setUpdate_date(article.getUpdate_date());
            articleInfoVo1.setId(article.getId());
            articleInfoVo1.setTitle(article.getTitle());
            articleInfoVo1.setContent(article.getContent());
            articlesVo.add(articleInfoVo1);
        });

        int totalMyArticles = articleInfoMapper.getTotalMyArticles(articleInfoVo);

        map.put("myArticleList",articlesVo);
        map.put("myArticleTotal",totalMyArticles);

        return map;
    }

    @Override
    public ArticleInfoVo getArticleDetail(Integer id) {
        ArticleInfoVo articleDetail = articleInfoMapper.getArticleDetail(id);
        String content =  articleContentMapper.getArticleContentById(id);
        articleDetail.setContent(content);
        return articleDetail;
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public void deleteArticle(Integer id) {
        articleInfoMapper.deleteArticle(id);
        articleContentMapper.deleArticleContent(id);
    }

    @Override
    public void toTop(Integer id) {
        articleInfoMapper.updataToTop(id);
    }

    @Override
    public void cancleTop(Integer id) {
        articleInfoMapper.updataCancleTop(id);
    }

    @Override
    public List<Map> getArticleListWithType(String articleTypeName) {
        List<Map> articleListWithType = articleInfoMapper.getArticleListWithType(articleTypeName);
        articleListWithType.forEach(p ->{
            Map map = new HashMap();
            map.put("blogId",p.get("id"));
            int praiseNum = articlePraiseMapper.selectPraiseNum(map);
            int praiseDownNum = articlePraiseMapper.selectPraiseDownNum(map);
            p.put("praiseNum",praiseNum);
            p.put("praiseDownNum",praiseDownNum);
        });
        return articleListWithType;
    }

    @Override
    public void praise(Map map) {
        ArticlePraiseEntity articlePraiseEntity = articlePraiseMapper.selectByBlogIdAndUsername(map);
        if(null == articlePraiseEntity){
            articlePraiseMapper.insertArticlePraise(map);
        }else{
            articlePraiseMapper.updateArticlePraise(map);
        }

    }

    @Override
    public ArticlePraiseEntity getPraise(Map<String, Object> map) {
        return  articlePraiseMapper.getPraise(map);
    }
}
